package com.victor.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Infomation;
import com.victor.entity.Tourismgl;
import com.victor.entity.TourismglXq;
import com.victor.entity.Tourismgltu;
import com.victor.service.TourismService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;
import com.victor.utils.OSSUtil;

@RestController
@RequestMapping("tourism")
public class TourismController {
	@Autowired
	private TourismService tourismService;
	@Autowired
	private LogController logcontroller;
	
	/**
	 * 添加旅游
	 * @param tourismgl
	 * @return
	 */
	@RequestMapping("/addtourism")
	public Msg addTourism(@RequestBody Tourismgl tourismgl) {
		String[] str = {"glclass","province","city","",""};
		boolean empty = ClassFilter.somePropertyIsEmpty(tourismgl, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean b = tourismService.insertTourism(tourismgl);
		if (tourismgl.getTourismglXqs() !=null && tourismgl.getTourismglXqs().size()>0) {
			for (int j = 0; j < tourismgl.getTourismglXqs().size(); j++) {
				tourismgl.getTourismglXqs().get(j).setGlid(String.valueOf(tourismgl.getId()));
				boolean b1 =tourismService.insertGongLve(tourismgl.getTourismglXqs().get(j));
				if (!b1) {
					return Msg.fail("添加攻略失败");
				}
				if (tourismgl.getTourismglXqs().get(j).getTourismgltu()!=null) {
						String glid= String.valueOf(tourismgl.getTourismglXqs().get(j).getId());
						tourismgl.getTourismglXqs().get(j).getTourismgltu().setGlid(glid);
						boolean b2 = tourismService.insertTuPian(tourismgl.getTourismglXqs().get(j).getTourismgltu());
						if (!b2) {
							return Msg.fail("添加攻略图失败");
						}
				}
			}
			
		}
		logcontroller.addLog(tourismgl.getAdminid(), "添加了旅游");
		return Msg.judge(b);
		
	}
	/**
	 * 添加攻略
	 * @param tourismglXq
	 * @return
	 */
	@RequestMapping("/addgonglve")
	public Msg addGongLve(@RequestBody TourismglXq tourismglXq) {
		if (tourismglXq == null || tourismglXq.getGlid()== null || tourismglXq.getGlid().isEmpty() || tourismglXq.getAdminid()==null || tourismglXq.getAdminid().isEmpty() ) {
			return Msg.fail("入参错误");
		}
		boolean b = tourismService.insertGongLve(tourismglXq);
		if (!b) {
			return Msg.fail("添加攻略失败");
		}
		boolean insertTuPian = tourismService.insertTuPian(tourismglXq.getTourismgltu());
		logcontroller.addLog(tourismglXq.getAdminid(), "添加旅程"+tourismglXq.getGlid()+"的攻略");
		return Msg.judge(insertTuPian);
	}
	/**
	 * 修改旅游
	 * @param tourismgl
	 * @return
	 */
	@RequestMapping("/updatetourism")
	public Msg updateTourism(Tourismgl tourismgl,String adminid) {
		if (tourismgl == null || tourismgl.getId() == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = tourismService.updateTourism(tourismgl);
		logcontroller.addLog(adminid, "修改了旅游"+tourismgl.getId());
		return Msg.judge(b);
	}
	/**
	 * 修改攻略
	 * @param tourismglXq
	 * @return
	 */
	@RequestMapping("/updategl")
	public Msg updateGonglve(TourismglXq tourismglXq,String adminid) {
		if (tourismglXq == null || tourismglXq.getId() == null ) {
			return Msg.fail("入参失败");
		}
		boolean b = tourismService.updateGongLve(tourismglXq);
		logcontroller.addLog(adminid, "修改了攻略"+tourismglXq.getId());
		return Msg.judge(b);
	}
	/**
	 * 修改图片
	 * @param tourismgltu
	 * @return
	 */
	@RequestMapping("/updatephoto")
	public Msg updatePhoto(Tourismgltu tourismgltu,String adminid) {
		if (tourismgltu == null || tourismgltu.getId() == null ) {
			return Msg.fail("入参错误");
		}
		Tourismgltu tu = tourismService.selPhotoById(tourismgltu.getId());
		OSSUtil.deleteFile(tu.getGlphoto());
		boolean b = tourismService.updatePhoto(tourismgltu);
		logcontroller.addLog(adminid, "修改了攻略图片"+tourismgltu.getId());
		return Msg.judge(b);
		
	}
	/**
	 * 删除旅游
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deletetourism")
	public Msg deleteTourism(int[] ids ,String adminid) {
		if (ids== null || ids.length<=0) {
			return Msg.fail("入参错误");
		}
		for (int i = 0; i < ids.length; i++) {
			Tourismgl tourismgl = tourismService.selTourismById(ids[i]);
			if (tourismgl != null) {
				for (int j = 0; j < tourismgl.getTourismglXqs().size(); j++) {
					OSSUtil.deleteFile(tourismgl.getTourismglXqs().get(i).getTourismgltu().getGlphoto());
				}
			}
			
		}
		logcontroller.addLog(adminid, "删除了旅程"+ids);
		boolean b = tourismService.delTourism(ids);
		return Msg.judge(b);
	}
	/**
	 * 删除攻略
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delgonglve")
	public Msg deleteGongLve(int[] ids,String adminid) {
		if (ids == null || ids.length<=0) {
			return Msg.fail("入参失败");
		}
		boolean b = tourismService.delGongLve(ids);
		logcontroller.addLog(adminid, "删除了攻略"+adminid);
		return Msg.judge(b);
		
	}
	/**
	 * 删除图片
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delphoto")
	public Msg deletePhoto(int[] ids,String adminid) {
		if (ids == null || ids.length<=0) {
			return Msg.fail("入参失败");
		}
		for (int i = 0; i < ids.length; i++) {
			List<Tourismgltu> list = tourismService.selPhotoByGlid(ids[i]);
			if (list != null && list.size()>0) {
				for (int j = 0; j < list.size(); j++) {
					OSSUtil.deleteFile(list.get(j).getGlphoto());
				}
			}
		}
		boolean b = tourismService.delPhoto(ids);
		logcontroller.addLog(adminid, "删除了图片"+ids);
		return Msg.judge(b);
		
	}
	/**
	 * 旅游列表
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/getall")
	public Msg selAll(int page,int row,String adminid) {
		PageInfo<Tourismgl> pageInfo = tourismService.selAll(page,row);
		logcontroller.addLog(adminid, "查看了旅游列表");
		return Msg.success().add("list", pageInfo);
		
	}
	/**
	 * 旅游详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/xiangqing")
	public Msg findXiangqing(Integer id,String adminid) {
		if (id == null ) {
			return Msg.fail("详情");
		}
		Tourismgl list = tourismService.selTourismById(id);
		logcontroller.addLog(adminid, "查看了"+id+"的详情");
		return Msg.success().add("list", list);
	}
	/**
	 * 搜索
	 * @param name
	 * @return
	 */
	@RequestMapping("/search")
	public Msg search(String name,String adminid) {
		if (name == null || name.isEmpty()) {
			return Msg.fail("入参失败");
		}
		Tourismgl tourismgl = tourismService.selTourismByName(name);
		logcontroller.addLog(adminid, "根据"+name+"搜索了旅游列表");
		return Msg.success().add("tourismgl", tourismgl);
		
	}
	
	/**
	 * 导出信息流
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/export")
	public void export(HttpServletRequest request,HttpServletResponse response,String adminid) throws Exception {
		List<Tourismgl> list = tourismService.selAllNoPage();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("旅游列表");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short)0, (short)4);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("旅游列表");
		
		Row headrow = sheet.createRow(2);
		headrow.createCell(0).setCellValue("名称");
		headrow.createCell(1).setCellValue("省");
		headrow.createCell(2).setCellValue("市");
		headrow.createCell(3).setCellValue("时间");
		
		for(Tourismgl tourismgl : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum()+1);
			if(tourismgl.getName()== null) {
				dataRow.createCell(0).setCellValue("");
			}else {
				dataRow.createCell(0).setCellValue(tourismgl.getName());
			}
			
			if(tourismgl.getProvince() == null) {
				dataRow.createCell(1).setCellValue("");
			}else {
				dataRow.createCell(1).setCellValue(tourismgl.getProvince());
			}
			if(tourismgl.getCity() == null) {
				dataRow.createCell(2).setCellValue("");
			}else {
				dataRow.createCell(2).setCellValue(tourismgl.getCity());
			}
			if(tourismgl.getTime() ==null) {
				dataRow.createCell(3).setCellValue("");
			}else {
				dataRow.createCell(3).setCellValue(tourismgl.getTime());
			}
		}
		String filename = "旅游列表";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
		logcontroller.addLog(adminid, "导出了旅程");
	}

}
